package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.Convert;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service("customerService")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerInfoFeignClient customerInfoClient;

    @Resource
    private RedisTemplate redisTemplate;

    /*
     * @param code 
     * @==============History===============<br/>;
     * @Description //   小程序授权登录
     * @Date 2024/7/31
     * @Author < a href="15991488967@163.com">chen</ a>;
     * @return java.lang.String
     */
    @Override
    public String login(String code) {
        //1 拿着code进行远程调用，返回用户id
        Result<Long> loginResult = customerInfoClient.login(code);

        //2 判断如果返回失败了，返回错误提示
        Integer codeResult = loginResult.getCode();
        if(codeResult != 200){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //3 获取远程调用返回用户id
        Long userId = loginResult.getData();

        //4 判断返回用户id是否为空，如果为空，返回错误提示
        if(userId == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        //5 生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-","");

        //6 把用户id放到Redis，设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                userId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.SECONDS);
        //7 返回token
        return token;
    }

    /*
     * @param customerId 用户id
     * @==============History===============<br/>;
     * @Description //   获取用户的登录信息
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {
        //根据用户id进行远程调用 得到用户信息
        Result<CustomerLoginVo> customerLoginVoResult =
                customerInfoClient.getCustomerLoginInfo(customerId);
        return getCustomerLoginVo(customerLoginVoResult);
    }

    @NotNull
    private CustomerLoginVo getCustomerLoginVo(Result<CustomerLoginVo> customerLoginVoResult) {
        Integer code = customerLoginVoResult.getCode();
        if(code != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        if(customerLoginVo == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        return customerLoginVo;
    }

    /*
     * @param token      token
     * @==============History===============<br/>;
     * @Description //   获取用户的登录信息
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        String customerId =
                (String) redisTemplate.opsForValue()
                        .get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);


        Result<CustomerLoginVo> result =
                customerInfoClient.getCustomerLoginInfo(Convert.toLong(customerId));

        return getCustomerLoginVo(result);
    }

    
    /*
     * @param updateWxPhoneForm 
     * @==============History===============<br/>;
     * @Description //   更新用户微信手机号
     * @Date 2024/8/1
     * @Author < a href="15991488967@163.com">chen</ a>;
     */
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> booleanResult = customerInfoClient.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }
}
